// pages/component/promotions/promotion/promotion.js
var GlobalData = require('../../../../utils/GlobalData.js');
var dataManager = require('../../../../utils/dataManager.js');
var Util = require('../../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    index: 0,
    date: '2016-09',
    time: '12:01',
    promoData: {},
    promoArr: [],
    isHaveData: false,
    year: "",
    month: "",
    isShare: false,
    hidden: true,
    kakuImg: "kaku1",
    isShowIntegral: false,
    isshowiphonex:false
  },

  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    if (GlobalData.isshowiphone) {
      this.setData({
        isshowiphonex: true
      })
    } else {
      this.setData({
        isshowiphonex: false
      })
    }
    //  ()
    if (app.getConfig().isShowIntegral) {
      this.setData({
        isShowIntegral: app.getConfig().isShowIntegral
      })
    }
    this.setData({
      hidden: true
    })
    // console.log(GlobalData.homeData)
    dataManager.removeBtnObserver("promotion")
    let dt = new Date();
    this.data.year = dt.getFullYear()
    let mon = dt.getMonth() + 1
    this.data.month = mon > 9 ? mon : "0" + mon;
    this.data.date = this.data.year + "-" + this.data.month;
    this.setData({
      year: this.data.year,
      month: this.data.month,
      date: this.data.date
    })
    // console.log(this.data.date)
    dataManager.rigisterObserver("promotion", this.onComplete, this);
    dataManager.getPromotion(this.data.year, this.data.month);
  },

  onComplete: function () {
    this.data.promoData = GlobalData.Promotion;
    this.data.promoArr = this.data.promoData.list;
    for (let i = 0; i < this.data.promoArr.length; i++) {
      this.data.promoArr[i]["rebate1"] = parseFloat(this.data.promoArr[i]["rebate1"]);
      this.data.promoArr[i]["totalprice1"] = parseFloat(this.data.promoArr[i]["totalprice1"]);
    }
    /**判断是否有数据 */
    this.setData({
      promoData: this.data.promoData,
      promoArr: this.data.promoArr
    })
    if (this.data.promoArr.length == 0)
      this.setData({
        isHaveData: true
      })
    else
      this.setData({
        isHaveData: true
      })
    // this.wxData = this.data && this.data.businessBehalf;
    // console.log(this.data.promoData, this.data.isHaveData, this.data.promoArr);
  },
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    var datetemp = e.detail.value
    // console.log("日历", e);
    this.onDateBlur(datetemp)
  },
  onDateBlur: function (date) {
    this.data.year = date.split('-')[0];
    this.data.month = date.split('-')[1];
    this.data.date = this.data.year + "-" + this.data.month;
    // this.data.date = 
    this.setData({
      year: this.data.year,
      month: this.data.month,
      date: this.data.date
    })
    dataManager.rigisterObserver("promotion", this.onComplete, this);
    dataManager.getPromotion(this.data.year, this.data.month);
  },
  /**前往卡库 */
  goCardDepots: function () {
    var that = this;
    this.setData({
      hidden: false
    })

    if (app.getConfig().isShowRecharge) {
      dataManager.UseNavigateTo('../../carddepots/carddepot/carddepot', true);
    } else
      /**前往海报系统 */
      dataManager.UseNavigateTo("/pages/template/entrepreneurship/entrepreneurship", true);
  },
  /**前往推广流程 */
  gopromotionProcess: function () {
    // if (GlobalData.istuiLc) {
    //   return;
    // }
    // GlobalData.istuiLc = true;
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../promotions/promoteProcess/promoteprocess', false);
    // dataManager.OnRequestPost({ method: "getparam", "token": GlobalData.token }, this.onPromoteProcess);

  },
  onPromoteProcess: function (data) {
    GlobalData.processTxt = data.AgentProcessContent;
    //  this.data = pkg.data
    if (dataManager.isClick("promoteprocess")) {
      return
    } else {
      dataManager.btnObserver("promoteprocess")
      dataManager.UseNavigateTo('../../promotions/promoteProcess/promoteprocess', false);
    }

  },
  goMall() {

      dataManager.UseNavigateTo("/pages/integralmall/integralmall", true)
  },
  /**前往主页 */
  goHome: function () {
    // wx.redirectTo({
    //   url: '../../homes/home/home'
    // // })
    // if (dataManager.isClick("home")) {
    //   return
    // } else {
    //   dataManager.btnObserver("home")
    this.setData({
      hidden: false
    })
    dataManager.UseNavigateTo('../../homes/home/home', true);
    // }

  },
  /**onShar分享 */
  onShare: function () {
    // console.log("分享");

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var img = ""
    if (app.getConfig().isShowRecharge)
      img = "kaku1";
    else
      img = "haibao1"

    this.setData({
      kakuImg: img,
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    // var user_id = wx.getStorageSync('uid')
    // console.log(this.data)
    // var user_id = this.data.user_id
    // var types = this.data.types
    // var id = this.data.id
    var zhekou = parseInt(GlobalData.homeData.levels)
    var title = "快来获取" + zhekou + "折购买房卡机会吧！"
    var desc = "我已" + zhekou + "折购人民棋牌房卡了，一起来吧。"
    let count = Math.round(0 + Math.random() * (5 - 0))
    var sharePIc = "https://shopapi.queyouquan.cn/images/share_" + Math.round(0 + Math.random() * (5 - 0)) + ".png"
    return {
      title: title,
      desc: desc,
      imageUrl: sharePIc,
      path: '/pages/template/showagent/showagent?sysno=' + GlobalData.homeData.sysno, //这里是分享带出去的参数，用户点击分享卡片是需要的参数
      success: function (res) {
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
        // 转发失败
      }
    }


    // return {
    //   title: '微信小程序联盟',
    //   desc: '最具人气的小程序开发联盟!',
    //   path: '/pages/component/logins/forgetPassword/forgetPassword?id=123'
    // }
  }
})